package com.niiwoo.activity.stage.enums.goods;

import java.util.ArrayList;
import java.util.List;

import com.niiwoo.tripod.provider.exception.NoSuchEnumValueException;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * Created by dengyunhui on 2017/11/23.
 * <p>
 * 商品类型枚举
 */
@Getter
@AllArgsConstructor
public enum GoodsTypeDicEnum {

    /**
     * 一级类型
     */
    QUAN(100, 0, "券类"),
    XUNI(101, 0, "虚拟类"),
    SHIWU(102, 0, "实物类"),
    DALIBAO(103, 0, "大礼包类"),
    QITA(104, 0, "其它"),

    /**
     * 券类
     */
    TIXIAN_HB(100010, 100, "提现红包"),
    XIANJIN_HB(100011, 100, "现金红包"),
    TOUZI_JXQ(100006, 100, "投资加息券"),
    DANBAO_FXQ(100007, 100, "担保返现红包"),
    TOUZI_FXQ(100008, 100, "投资返现红包"),
    JIEKUAN_FXQ(100009, 100, "借款返现红包"),
    JIEKUAN_LILV_ZKK(100012, 100, "借款利率折扣卡"),

    DANBAO_LCJ(100001, 100, "担保理财金"),
    TOUZI_LCJ(100002, 100, "投资理财金"),

    /**
     * 虚拟类
     */
    JIFEN_KA(101004, 101, "积分"),
    BUQIAN_KA(101006, 101, "签到补签卡"),
    CHOUJIANG_KA(101007, 101, "抽奖卡");

    private Integer id;
    private Integer parentId;
    private String typeName;

    public static GoodsTypeDicEnum enumOf(Integer id) {
        for (GoodsTypeDicEnum goodsTypeDicEnum : values()) {
            if (goodsTypeDicEnum.getId().equals(id)) {
                return goodsTypeDicEnum;
            }
        }
        throw new NoSuchEnumValueException("GoodsTypeDicEnum:" + id);
    }

    /**
     * 获取子类型的枚举
     *
     * @return
     */
    public static List<GoodsTypeDicEnum> getChildTypeList(Integer parentId) {
        List<GoodsTypeDicEnum> list = new ArrayList<>();
        for (GoodsTypeDicEnum r : GoodsTypeDicEnum.values()) {
            if (r.parentId.equals(parentId)) {
                list.add(r);
            }
        }
        return list;
    }

    /**
     * 递归获取顶级枚举
     *
     * @param id
     * @return
     */
    public static GoodsTypeDicEnum getTopGoodsTypeDicEnum(Integer id) {
        if (id == null) {
            return null;
        }
        for (GoodsTypeDicEnum goodsTypeDicEnum : GoodsTypeDicEnum.values()) {
            if (goodsTypeDicEnum.getId().equals(id)) {
                if (goodsTypeDicEnum.getParentId().equals(0)) {
                    return goodsTypeDicEnum;
                } else {
                    return getTopGoodsTypeDicEnum(goodsTypeDicEnum.getParentId());
                }
            }
        }
        return null;
    }
}
